$(function () {

    var productId = $("#productId").val();
    var barCode = $("#barCode").val();


    $("#addToCart").on('click',function (event) {
        //添加到购物车的商品，只获取到barCode就可以了
        //barCode 根据productId ，和所选的颜色以及尺码进行拼接
        var colorCode = $('li[data-type=0][class="guigeCur"]').attr('data');
        var sizeCode = $('li[data-type=1][class="guigeCur"]').attr('data');
        var barCode=productId+colorCode+sizeCode;
        $.ajax({
            url:"/cart/addToCart/"+barCode,
            success:function (datas) {
                alert("添加成功");
            }
        })
    });





    //点击切换属性
    $('body').on('click','li[data-name=attribute]',function (event) {
        //获取当前点击元素的 type 和 value
        var selectType = $(event.currentTarget).attr('data-type');
        var selectValue = $(event.currentTarget).attr('data');
        //重新拼barCode
        var newBarCode ='';

        //只是换颜色的时候才去请求后台
        if(selectType==='0'){
            //换的是颜色
            var sizeCode = barCode.slice(-3);
            newBarCode = productId+selectValue+sizeCode;
        }else {
            //换的是尺码
            var colorCode  = barCode.slice(-6,-3);
            newBarCode = productId+colorCode+selectValue;
        }

        window.location.href="/product/productDetail/"+newBarCode;


    })

    getProductDesc();

    //获取产品描述
    function getProductDesc() {
        $.ajax({
            url:"/product/getProductDesc/"+productId,
            success:function (datas) {
                var descHtml ='';

            }
        })
    }

    initWxShare();

    //微信分享的
    function initWxShare() {
        if (!window.wx) {
            return;
        }
        var regParm = window.location.href.split('#')[0];//后台签名的url一定是使用jssdk的当前页面的完整url除去'#'部分
        $.ajax({
            url: "/wxRedirct?url=" + regParm,
            type: "GET",
            success: function (datas) {
                if ("" != datas) {
                    var data = eval("(" + datas + ")");
                    wx.config({
                        debug: false,
                        appId: data.appId,
                        timestamp: data.timestamp,
                        nonceStr: data.nonceStr,
                        signature: data.signature,
                        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ'],
                        success: function (el) {
                        }, fail: function (el) {
                        }
                    });
                    wx.ready(function () {
                        wx.checkJsApi({
                            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ'],
                            success: function (res) {
                                var shareData = {
                                    title: $("#productTitle").val(),
                                    desc: $("#productDesc").val(),
                                    link: window.location.href.split('#')[0],
                                    imgUrl: $("#productImag").val(),
                                    type: 'link',
                                    dataUrl: '',
                                    success: function () {
                                        alert('分享成功！');
                                    },
                                    fail: function () {
                                        alert("分享失败，请重试！");
                                    },
                                    cancel: function () {
                                        alert("您取消了分享");
                                    }
                                };
                                wx.onMenuShareAppMessage(shareData);
                                wx.onMenuShareTimeline(shareData);
                                wx.onMenuShareQQ(shareData);
                            },
                            fail: function () {
                                alert('您的微信当前版本不支持此分享功能!');
                            }
                        });
                    });
                }

            }
        });

    };

})